package hp.com.servlet.CartDataServlet;

import hp.com.entity.CartData;
import hp.com.service.CartDataService;
import hp.com.service.impl.CartDataServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/CartPriceAjax")
public class CartPriceAjax extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int dataId = Integer.parseInt(req.getParameter("dataId"));
        //获取当前cart数据对象
        CartDataService cs = new CartDataServiceImpl();
        CartData data=null;
        try{data = cs.GetCartDataById(dataId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //获取新的数量
        String temp = req.getParameter("newAmount");
            int newAmount = Integer.parseInt(temp);

            double result=0;
            try{
                result=(double) Math.round(cs.UpdateAmount(data,newAmount) * 100) / 100;
            } catch (Exception ee){
                ee.printStackTrace();
            }
            resp.getWriter().print(result);
        }
    }

